﻿using UnityEngine;
using System.Collections;
using System;

public class GameTimer:IGameTimer
{
    /// <summary>
    /// 边缘时间，超出结束计时
    /// </summary>
    public float EdgeTime { get; set; }
    /// <summary>
    /// 当期的时间
    /// </summary>
    public float CurrentTime { get; private set; }
    /// <summary>
    /// 当计时完成时
    /// </summary>
    public Action OnTimeOut { get; set; }
    /// <summary>
    /// 计时器是否被暂停
    /// </summary>
    public bool IsPause { get; set; }
    /// <summary>
    /// 当暂停时
    /// </summary>
    public Action OnPause { get; set; }
    /// <summary>
    /// 是否计时完成
    /// </summary>
    public bool IsTimeOut { get; set; }
    /// <summary>
    /// 时间的缩放
    /// </summary>
    public float TimeScale { get; set; }

    /// <summary>
    /// 是否被打断
    /// </summary>
    public bool IsBreak {get; set;}

    public string InstId { get; set; }

    public GameTimer()
    {

    }

  public  void SetData(float edgeTime, Action onTimeOut, float timeScale = 1)
    {
        EdgeTime = edgeTime;
        OnTimeOut += onTimeOut;
        TimeScale = timeScale;
    }

    public void Init()
    {

    }

    public void SetVisual(bool isActive)
    {

    }

    public void Polling(float deltaTime)
    {
        if (!IsPause)
        {
            CurrentTime += deltaTime * TimeScale;
            if (IsBreak)
            {
                CurrentTime += EdgeTime + 1;
            }
            if (CurrentTime > EdgeTime)
            {
                if (!IsBreak)
                {
                    OnTimeOut();
                }
                else
                {
                    IsBreak = false;
                }
           
                CurrentTime = 0;
                IsTimeOut = true;
                Dispose();
            }
        }
    }

    public void Dispose()
    {
        OnTimeOut -=OnTimeOut;
    }


}
